function addCommas(nStr)
{
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ',' + '$2');
  }
  return x1 + x2;
}

function days_between(date1, date2) {

    // The number of milliseconds in one day
    var ONE_DAY = 1000 * 60 * 60 * 24

    // Convert both dates to milliseconds
    var date1_ms = date1.getTime()
    var date2_ms = date2.getTime()

    // Calculate the difference in milliseconds
    var difference_ms = Math.abs(date1_ms - date2_ms)
    
    // Convert back to days and return
    return Math.round(difference_ms/ONE_DAY)

}

function roudNumber(number){
	return parseInt(number*100)/100;
}

function UpdateDates(startDate, endDate){
    var tempSD, tempED, sd, ed, dl, middle=false, currentDate;
    tempSD = startDate.split('/');
    tempED = endDate.split('/');
    sd = new Date();
    ed = new Date();

    sd.setFullYear(parseInt(tempSD[2], 10), parseInt(tempSD[0], 10) - 1, parseInt(tempSD[1], 10));
    ed.setFullYear(parseInt(tempED[2], 10), parseInt(tempED[0], 10) - 1, parseInt(tempED[1], 10));
    currentDate = new Date();
	if((sd < currentDate)&&(ed>currentDate)){
		middle = true;
	}    
	var tempA = new Date(sd.getFullYear(), sd.getMonth(), sd.getDate());
	var tempB = new Date(ed.getFullYear(), ed.getMonth(), ed.getDate());
	while(!middle){
		
		if((tempA < currentDate)&&(tempB>currentDate)){
			middle = true;
		} else{
			tempA = new Date(ed.getFullYear(), ed.getMonth(), ed.getDate());
			if(sd.getMonth() === 11){
				var tempB = new Date(sd.getFullYear() + 1, 0, sd.getDate());
			}else{
				var tempB = new Date(sd.getFullYear(), sd.getMonth() + 1, sd.getDate());
			}
			sd = new Date(tempA.getFullYear(), tempA.getMonth(), tempA.getDate());
			ed = new Date(tempB.getFullYear(), tempB.getMonth(), tempB.getDate());
		} 
	}
    dl = days_between(ed, new Date());
    return {
        startDate:((sd.getMonth() + 1) < 10 ? "0" + (sd.getMonth() + 1) : (sd.getMonth() + 1)) + '/' + (sd.getDate() < 10 ? "0" + sd.getDate() : sd.getDate()) + '/' + sd.getFullYear(),
        endDate:((ed.getMonth() + 1) < 10 ? "0" + (ed.getMonth() + 1) : (ed.getMonth() + 1)) + '/' + (ed.getDate() < 10 ? "0" + ed.getDate() : ed.getDate()) + '/' + ed.getFullYear(),
        daysBetween: (dl===0 ? 1 : dl)
    }
}

